package com.heima.search.listener;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SyncArticleListener {

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @KafkaListener(topics = "topicB")
    public void syncArticle(ConsumerRecord<String,String> msg) throws IOException {

        //接收参数
        String json = msg.value();
        String id = msg.key();
        //同步es
        IndexRequest indexRequest = new IndexRequest("app_info_article");
        indexRequest.source(json, XContentType.JSON);
        indexRequest.id(id);
        restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

    }

}
